<template>
  <el-dialog
    v-model="visible"
    :close-on-click-modal="false"
    title="添加客户"
    width="560px"
    top="50px"
  >
    <el-form
      ref="formRef"
      :model="form"
      label-width="100px"
      :rules="rules"
    >
      <el-form-item label="客户手机号" prop="phone">
        <el-input
          v-model="form.phone"
          placeholder="请输入客户手机号"
          maxlength="11"
          show-word-limit
          oninput="value=value.replace(/[^\d]/g,'')"
        />
      </el-form-item>
      <el-form-item label="客户昵称">
        <el-input
          v-model="form.name"
          placeholder="请输入客户昵称"
          maxlength="10"
          onblur="value=value.replace(/(^\s*)|(\s*$)/g, '')"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生日">
        <el-date-picker
          v-model="form.birthday"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="标签">
        <el-select
          v-model="form.ids"
          multiple
          collapse-tags
          placeholder="请选择"
        >
          <el-option
            v-for="item in tagList"
            :key="item.labelId"
            :label="item.labelName"
            :value="item.labelId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="form.remark"
          maxlength="200"
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button
        type="primary"
        @click="handleSave"
      >保 存</el-button>
      <el-button
        @click="handleClose"
      >取 消</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, shallowRef, toRefs } from 'vue';

import {
  addCustomer,
} from '@/api/customer'

const props = defineProps({
  tagList: {
    type: Object,
    default: () => ({})
  }
})
let { tagList } = toRefs(props);
const emits = defineEmits(['refresh'])

let visible = shallowRef(false);
const formRef = ref();
let form = ref({
  birthday: '',
  buyerUserId: '',
  ids: [],
  name: '',
  phone: '',
  remark: '',
  sex: '',
});
const rules = {
  phone: [
    { required: true, validator: validPhone, trigger: 'blur' },
  ],
}
function validPhone (rule, value, callback) {
  if (!value) {
    return callback(new Error('请输入客户手机号'))
  }
  if (!/^[1][3,4,5,7,8,9][0-9]{9}$/.test(form.value.phone)) {
    return callback(new Error('请输入正确的手机号'))
  }
  callback()
}
function show () {
  visible.value = true
}
function handleSave () {
  formRef.value.validate((valid) => {
    if (valid) {
      addCustomer(form.value).then((res) => {
        if (res.code === '') {
          ElMessage.success('添加成功')
          emits('refresh')
          handleClose()
        } else {
          ElMessage.error(res.message)
        }
      })
    }
  })
}
function handleClose () {
  visible.value = false
  formRef.value.resetFields();
}

defineExpose({
  show
})
</script>
